<?php
class Add_model extends Model {

    function Add_model()
    {
        parent::Model();
        $this->load->database();
    }

    /**
     * Add User
     *
     * this adds a user to the database with user privilages. encryption of
     * password
     *
     * @param string $name
     * @param string $email
     * @param string $pass
     */
    function add_user($name,$email,$pass)//take in user info from register form
    {
        $this->load->library('encrypt');

        //add user to the user table
        $data = array(
               'Name' => $name ,
               'Email' => $email ,
               'Password' => $this->encrypt->sha1($pass)
            );

        $this->db->insert('user', $data);
    }

    /**
     * Add Item
     *
     * adds an item to the database with a user id taken from
     * the users logged in ID
     *
     * @param string $img
     * @param string $note
     * @param int $uid
     */
    function add_item($img, $note, $uid)//take in user id and itme info
    {
        //add item to the item table associate with user
        $data = array(
               'Image' => $img ,
               'Note' => $note ,
               'UID' => $uid
            );

        $this->db->insert('item', $data);
        //call add image to store image
    }

    /**
     *  Add Vote
     *
     * this adds a vote to the database associated with the item id
     * passed via the main page vote form
     *
     * @param int $id
     */
    function add_vote($id)//take in item id
    {
        //add vote to item table associated with itemid
        $data = array(
               'IID' => $id
            );

        $this->db->insert('vote', $data);
    }

    /**
     * Add Image
     *
     * this takes in an image from the add item form. it adds the image to
     * the images folder and then passes the user information to the
     * add item method
     */
    function add_image()
    {
        //upload a file from the upload form
            $this->load->helper(array('form', 'url'));
            $file = $this->input->post('userfile');

            //FILE UPLOAD STUFF
            $config['upload_path'] = './image/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']     = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $upload_data = $this->upload->data();
                $img = $upload_data->file_name;

                $this->load->add_model();
                $this->add_model->add_item($img, $note, $uid);
            }

    }

}
//EOF
